package uplift.project;

import uplift.project.model.VolunteerEvent;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;

public class PreviewEventActivity extends Activity {
	
	private TextView eventName;
	private TextView eventCategory;
	private TextView eventDate;
	private TextView eventLocation;
	private TextView eventTime;
	private TextView eventDescription;
	
	private Bundle oldExtras;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preview_event);
        
        Intent startIntent = this.getIntent();
        Bundle extras = startIntent.getExtras();
        String name = extras.getString("name");
        String category = extras.getString("category");
        String location = extras.getString("location");
        String date = extras.getString("date");
        String startTime = extras.getString("starts");
        String endTime = extras.getString("ends");
        String description = extras.getString("description");
        oldExtras = extras.getBundle("bundle");

        eventName = (TextView) this.findViewById(R.id.textView_previewEventName);
        eventCategory = (TextView) this.findViewById(R.id.textView_previewCategory);
        eventDate = (TextView) this.findViewById(R.id.textView_previewDate);
        eventLocation = (TextView) this.findViewById(R.id.textView_previewLocation);
        eventTime = (TextView) this.findViewById(R.id.textView_previewTime);
        eventDescription = (TextView) this.findViewById(R.id.textView_previewDescription);
        
        eventName.setText(name);
        eventCategory.setText(category);
        eventDate.setText(date);
        eventLocation.setText(location);
        eventTime.setText(startTime + " - " + endTime);
        eventDescription.setText(description);
        eventDescription.setMovementMethod(new ScrollingMovementMethod());
        
		// set the action bar to have up-navigation enabled
	    ActionBar actionBar = getActionBar();
	    actionBar.setDisplayHomeAsUpEnabled(true);
    }
    
    // Goes to event submission screen
    public void submitEvent(View v) {

		confirmBox("Submit Event", "You successfully created event " + eventName.getText() + ".", v);
    }
    
    
    protected void confirmBox(String title, String message, final View v) {
    	new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog))
        .setMessage(message)
        .setTitle(title)
        .setPositiveButton("OK",
     		   new DialogInterface.OnClickListener() {
 		           public void onClick(DialogInterface dialog, int whichButton){
 		        	  Intent i = new Intent(v.getContext(), UpliftActivity.class);
 		         	
 		         	String[] times = eventTime.getText().toString().split(" - ");
 		         	
 		     		ApplicationData appState = ((ApplicationData)PreviewEventActivity.this.getApplication());
 		     		
 		     		if (oldExtras != null) {
 		     			appState.eventsList.removeEvent(oldExtras.getString("name"),
 		     					oldExtras.getString("category"),
 		     					oldExtras.getString("location"),
 		     					oldExtras.getString("date"),
 		     					oldExtras.getString("starts"),
 		     					oldExtras.getString("ends"),
 		     					oldExtras.getString("description"));
 		     		}
 		     		
 		     		
 		     		VolunteerEvent ve = new VolunteerEvent(eventName.getText().toString(),
 		     												eventCategory.getText().toString(),
 		     												eventLocation.getText().toString(),
 		     												eventDate.getText().toString(),
 		     												times[0],
 		     												times[1],
 		     												eventDescription.getText().toString(),
 		     												true, true);
 		     		Log.v("boo", ve.toString());
 		     		appState.eventsList.addEvent(ve);
 		         	
 		         	i.putExtra("name", ((TextView) PreviewEventActivity.this.findViewById(R.id.textView_previewEventName)).getText().toString());
 		         	
 		         	startActivity(i);
 		           }
 		       })
 		.show();
    }
    
    // Goes back to the create event screen
    public void editEvent(View v) {
    	finish();
    }
    
    // handles action item button presses
 	@Override
 	public boolean onOptionsItemSelected(MenuItem item) {
 		if(item.getItemId() == android.R.id.home) {
 			// move up the hierarchy

 			Intent i = new Intent(this.getApplicationContext(), UpliftActivity.class);
 			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 			startActivity(i);
 		} else if(item.getItemId() == R.id.menu_home) {
 			// go to Home screen
 			
 			Intent i = new Intent(this.getApplicationContext(), UpliftActivity.class);
 			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 			startActivity(i);
 		} else if(item.getItemId() == R.id.menu_my_events) {
 			// go to My Events screen
 			
 			Intent i = new Intent(this.getApplicationContext(), MyEventsTabActivity.class);
 			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 			startActivity(i);
 		}
 		return true;
 	}
 	
 	// set up Action Bar
 	@Override
 	public boolean onCreateOptionsMenu(Menu menu) {
 	    MenuInflater inflater = getMenuInflater();
 	    inflater.inflate(R.menu.action_bar, menu);
 	    return true;
 	}
}